package com.yq.spbYq.controller;

import com.yq.spbYq.Reward;
import com.yq.spbYq.service.impl.RewardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/rewards")
public class RewardController {

    @Autowired
    private RewardService rewardService;

    // 获取所有奖励物品
    @GetMapping("/getAll")
    public ResponseEntity<List<Reward>> getAllRewards() {
        List<Reward> rewards = rewardService.getAllRewards();
        return ResponseEntity.ok(rewards);
    }

    // 根据ID获取奖励物品
    @GetMapping("/getById/{rewardId}")
    public ResponseEntity<Reward> getRewardById(@PathVariable Integer rewardId) {
        Reward reward = rewardService.getRewardById(rewardId);
        if (reward != null) {
            return ResponseEntity.ok(reward);
        } else {
            return ResponseEntity.notFound().build();
        }
    }

    // 添加奖励物品
    @PostMapping("/add")
    public ResponseEntity<Reward> addReward(@RequestBody Reward reward) {
        int result = rewardService.addReward(reward);
        if (result > 0) {
            return ResponseEntity.ok(reward);
        } else {
            return ResponseEntity.badRequest().build();
        }
    }

    // 更新奖励物品
    @PutMapping("/update")
    public ResponseEntity<Reward> updateReward(@RequestBody Reward reward) {
        int result = rewardService.updateReward(reward);
        if (result > 0) {
            return ResponseEntity.ok(reward);
        } else {
            return ResponseEntity.badRequest().build();
        }
    }

    // 根据ID删除奖励物品
    @DeleteMapping("/delete/{rewardId}")
    public ResponseEntity<Void> deleteRewardById(@PathVariable Integer rewardId) {
        int result = rewardService.deleteRewardById(rewardId);
        if (result > 0) {
            return ResponseEntity.ok().build();
        } else {
            return ResponseEntity.notFound().build();
        }
    }
}